#!/usr/bin/python
##################################################

##################################################
import os, sys, string, commands, calendar, datetime

class XXDownload:
	def __init__(self):
		pass
	
	# start download
	# @param dldir download dir
	# @param dllistfile download list file
	# @param logfile download log file
	def start(self, dldir, dllistfile, logfile):
		today = datetime.date.today()
		if (self.isRunning()):
			return
		
		cmd = 'wget -p ' + dldir + ' -c -b -i ' + dllistfile + ' -o ' + logfile
		result = commands.getstatusoutput(cmd)
		
	# Check auto download is running
	def isRunning(self):
		result = commands.getstatusoutput('ps -ef | grep xxautodl.py')
		#print len(result)
		if (len(result) > 2):
			return True
		else:
			return False
		
	def printHelp(self, cmd):
		print 'Usage:'
		print '    ' + cmd + ' dldir dllistfile logfile'
		print 'e.g.'
		print '    ' + cmd + ' /incoming /incoming/dllist /incoming/dl.log' 
		
t = XXDownload()
if len(sys.argv) != 4:
	t.printHelp(sys.argv[0])
else:
	dldir = sys.argv[1]
	dllistfile = sys.argv[2]
	logfile = sys.argv[3]
	t.start(dldir, dllistfile, logfile)

